import { ref } from 'vue';

import { useCommonStore } from '#/store';

const commonStore = useCommonStore();

export interface RowType {
  id: string;
  digitalInvoiceNumber: string;
  invoiceNumber: string;
  invoiceCode: string;
  invoiceType: string;
  invoiceDate: string;
  sellerName: string;
  buyerName: string;
  goodsOrServiceName: string;
  amount: string;
  taxRate: string;
  taxAmount: string;
  totalAmount: string;
  invoiceSource: string;
  invoiceStatus: string;
  voucherNo: string;
  source: string;
}

interface SearchFormData {
  start: string;
  end: string;
  startEndRangeTimeValue: [string, string];
  type: string;
  customerId?: string;
}

export const searchFormData = ref<SearchFormData>({
  start: '',
  end: '',
  startEndRangeTimeValue: ['', ''],
  type: 'OUTPUT',
  customerId: commonStore.customerInfo?.id,
});

interface IStatisticsData {
  amountSum: number;
  amountSumTitle: string;
  taxAmountSum: number;
  taxAmountSumTitle: string;
  totalAmountSum: number;
  totalAmountSumTitle: string;
}

export const statisticsData = ref<IStatisticsData>({
  amountSumTitle: '金额合计',
  taxAmountSumTitle: '税额合计',
  totalAmountSumTitle: '价税合计',
  amountSum: 0,
  taxAmountSum: 0,
  totalAmountSum: 0,
});

export function buildQueryParams({
  page = { currentPage: 1, pageSize: 20 },
  formValues = {},
  searchFormData = {} as SearchFormData,
}) {
  const params = {
    pageNum: page.currentPage,
    pageSize: page.pageSize,
    ...formValues,
    ...searchFormData,
  };

  const [startDate = '', endDate = ''] = params.startEndRangeTimeValue || [];
  if (startDate) params.start = `${startDate} 00:00:00`;
  if (endDate) params.end = `${endDate} 23:59:59`;
  Reflect.deleteProperty(params, 'startEndRangeTimeValue');
  return params;
}
